home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15671 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  58 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: Philippe Verdy <100105.3120@compuserve.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Is it OK to delete const *type pointers?
  5. Date: 7 Apr 1996 22:39:40 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4k9g7c$fl9@arl-news-svc-5.compuserve.com>
  8. NNTP-Posting-Host: ad04-110.compuserve.com
  9.  
  10. Enno Sandner <enno@intellektik.informatik.th-darmstadt.de> s'Θcrit :
  11. > Acme Instant Dehydrated Boulder Kit wrote:
  12. > > 
  13. > > Some compilers let me do this, others do not. What I would like to know is,
  14. > > what does Stroustrup think? I.e. is it written somewhere in the ARM (I checked
  15. > > but can't find it) or in the draft standard? If somoene could e-mail me a
  16. > > quote or a pointer to a place where I can read where it says such a thing
  17. > > should or should not be allowed, I would appreciate it!
  18. > > 
  19. > > const int* array= new int[30];
  20. > > delete[] array;
  21. > > 
  22. > > MSVC says "can not delete const*"
  23. > > Some versions of G++ also don't let me do this.
  24. > > 
  25. > > Is deleting an object technically considered changing that object's value?
  26. > > 
  27. > The standard doesn't specify this (5.3.5.4):
  28. > ... It is unspecified whether the deletion of an object changes its
  29. >     value. ...
  30. > According to the DWP a dtor can be invoked even for a const-object
  31. > (12.4 class.dtor). Thus I would say the above code should compile.
  32. >     Enno
  33.  
  34. The following compiles under BC++ 4.0, 4.5, and 5.0:
  35. int const *ptr = new int(1);
  36. int const *arr = new int[10];
  37. main()
  38. {
  39.   delete ptr;
  40.   delete[] arr;
  41. }
  42.  
  43. Being const does not mean the object is read-only. Nor does
  44. it mean that its value won't change. Nor does it mean that
  45. it cannot be deleted.
  46. It only forbids writing to it directly without an
  47. explicit const_cast<>. This cast however is an operator
  48. which can be overriden to enforce const-ness, by returning
  49. a non-const copy of the referenced object.
  50.  
  51. Const-ness is a security helper, which also avoids unnecessary
  52. invocation of copy constructors, while specifying more
  53. precisely the intended objects interface and the set of
  54. operations which can occur on them.
  55.